package com.sun.androidexerciseproject.function.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.text.TextUtils;

import com.sun.androidexerciseproject.function.app.MyApplication;

import java.io.Closeable;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Description:
 * File: FileUtil.java
 * Author: Administrator
 * Create: 2019/6/20 0020 上午 11:48
 */
public class FileUtil {
    public static final String FILE_SUFFIX = ".log";
    public static final String FILE_LOG_PREFIX = "log_";
    public static final String FILE_LOG_FOLDER = "log";
    private static final String STORAGE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() +
            File.separator + MyApplication.getContext().getPackageName();
    private static final ThreadLocal<SimpleDateFormat> S_DATE_FORMAT = new ThreadLocal<SimpleDateFormat>() {
        @SuppressLint("SimpleDateFormat")
        @Override
        protected SimpleDateFormat initialValue() {
            return new SimpleDateFormat("yyyy-MM-dd");
        }
    };

    /**
     * 保存log到外置存储卡
     *
     * @param message
     */
    public static void saveLogExternal(String message) {
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {
            String fileName = getCurLogFileName();
            String path = STORAGE_PATH;
            String newFileStr = path + File.separator + fileName;
            FileWriter fileWriter = null;
            try {
                if (!isFileExist(newFileStr)) {
                    File newFile = new File(newFileStr);
                    createDir(newFile.getParent());
                    newFile.createNewFile();
                }
                File file = new File(path, fileName);
                fileWriter = new FileWriter(file, true);
                fileWriter.write(message + "\n");
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                quietClose(fileWriter);
            }

        }
    }

    /**
     * 内部保存日志
     *
     * @param message
     */
    public static void saveLogInternal(String message) {
        message += "\n";
        String fileName = getCurLogFileName();
        try {
            Context context = MyApplication.getContext();
            String path = context.getFilesDir().getAbsolutePath() + File.separator + FILE_LOG_FOLDER;
            String newFileStr = path + File.separator + fileName;
            if (!isFileExist(newFileStr)) {
                File newFile = new File(newFileStr);
                createDir(newFile.getParent());
                newFile.createNewFile();
            }
            FileOutputStream fileOutputStream = new FileOutputStream(newFileStr, true);
            fileOutputStream.write(message.getBytes());
            fileOutputStream.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getNowDate() {
        return S_DATE_FORMAT.get().format(new Date());
    }

    public static String getCurLogFileName() {
        return FILE_LOG_PREFIX + getNowDate() + FILE_SUFFIX;
    }

    private static void quietClose(Closeable closeable) {
        if (null != closeable) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static boolean isFileExist(String fileName) {
        if (TextUtils.isEmpty(fileName)) {
            return false;
        }
        File file = new File(fileName);
        return file.exists() && file.isFile();
    }

    private static boolean createDir(String path) {
        File dirFile = new File(path);
        return dirFile.exists() || dirFile.mkdirs();
    }
}
